package player

import (
	"GameServer/Common/data"
	"GameServer/Common/utils"
	"GameServer/Game/errors"
	"GameServer/Game/lib"
	"GameServer/Game/sp"
)

// 获取每日礼包道具
func (p *Player) DailyGiftReqRequest(req *sp.DailyGiftReqRequest) (res *sp.DailyGiftReqResponse) {
	us := p.User

	uNewGuide := us.GetNewGuide()
	// 新手引导未完成，每日礼包不能获取
	if !uNewGuide.SlotFlag {
		res = &sp.DailyGiftReqResponse{}
		return
	}
	dg, err := lib.GetDailyGift(us)
	if err != nil {
		res = &sp.DailyGiftReqResponse{
			Errcode: errors.ErrCode(err),
		}
		return
	}

	res = &sp.DailyGiftReqResponse{
		DailyGiftInfo: lib.FormatDailyGiftSp(dg),
	}
	return
}

// 8401 购买每日礼包道具
func (p *Player) DailyGiftBuyRequest(req *sp.DailyGiftBuyRequest) (res *sp.DailyGiftBuyResponse) {
	buyingPackId, dg, uProp, resGoods, boxRes, err := lib.BuyDailyGift("DailyGiftBuyRequest", p.User, p.Send)
	if err != nil {
		res = &sp.DailyGiftBuyResponse{
			Errcode: errors.ErrCode(err),
		}
		return
	}
	buyingPackConfig, err := lib.GetPaidPropsPacksConfig(buyingPackId)
	if err == nil {
		// 首充状态设置
		if p.User.FirstReChargeStatus == 0 && int64(buyingPackConfig.Typ) != data.PaidPropsEnum.FreePack {
			p.User.SetFirstRechargeStatus(1)
		}
	}
	res = &sp.DailyGiftBuyResponse{
		Errcode:             0,
		DailyGiftInfo:       lib.FormatDailyGiftSp(dg),
		Res:                 lib.FormatGoodsInfoSps(resGoods),
		BoxRes:              lib.FormatBoxResSps(boxRes),
		FirstReChargeStatus: p.User.FirstReChargeStatus,
	}
	p.TimelyUpdateProp(req.GetType(), uProp)
	utils.AsyncFunc(func() {
		p.UpdateProp(req.GetType(), uProp)
	})
	return
}
